﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


#region AddingNewScreens
//To add a new screen all you need to do is add the name into enum ImagineScreens
//and then register it in PowerUpTheGame() with the ScreenManager
//Now you can call the SetScreen() from anywhere.
#endregion

namespace Imagine
{
    public enum eImagineScreens
    {
        eInitScreen,
        eMenuScreen,
        eGamePlayScreen,        //This wil be the main screen where all the fun starts
        eCreateSessionScreen,
        eFindSessionScreen,
        eLobbyScreen,
        eSelectionScreen
        //More Screens to be added later
    }

    public class TheScreenManager
    {
        GameScreen m_ActiveScreen;          //The Current Screen
        GameScreen m_InActiveScreen;        //The Screen in the background, Eg: when game will be paused
        Dictionary<int, GameScreen> ScreenMap;

        static public TheScreenManager Instance()
        {
            if(m_instance == null)  m_instance = new TheScreenManager();
            
            return m_instance;
        }

        #region Private_Constructor
        private TheScreenManager()
        {
            //So that both screens are not null and i dont haveto check for initial Null condition
            m_ActiveScreen = new GameScreen();
            m_InActiveScreen = new GameScreen();
            ScreenMap = new Dictionary<int,GameScreen>();
        }
        static TheScreenManager m_instance;
        #endregion

        public void SetScreen(int screen, eScreenState previousScreenState/*What to do with the Previous Screen*/, object extraData)
        {
            if(previousScreenState == eScreenState.DEAD)
                m_ActiveScreen.Release();
            else     //For Active and Inactive, we dont need to release the state
            {
                m_InActiveScreen = m_ActiveScreen;
                m_InActiveScreen.CurrentState = previousScreenState;
            }

            //Now finally assign the new screen
            ScreenMap.TryGetValue(screen, out m_ActiveScreen);
            m_ActiveScreen.Init(extraData);
          
        }

        public void RegisterScreen(int screenKey, GameScreen screen)
        {
            //I dont need to make sure that a screen is not registered twice
            //So no check for that
            //I am the only programmer here, I dont need to babysit myself :p
            ScreenMap.Add(screenKey,screen);
        }
        
        public GameScreen ActiveScreen 
        {
            get { return m_ActiveScreen;    } 
        }

        public GameScreen InActiveScreen
        {
            get { return m_InActiveScreen;  }
        }
        
       public bool Update(GameTime gameTime)    
       {  
           if(m_InActiveScreen.CurrentState == eScreenState.ACTIVE)
               m_InActiveScreen.Update(gameTime);

           return m_ActiveScreen.Update(gameTime); 
       } 
        
       public bool Draw(GameTime gameTime, SpriteBatch _spriteBatch)    
       { 
            //We need to draw the Inactive Screen First, if needed be
            if(m_InActiveScreen.CurrentState != eScreenState.DEAD)
                m_InActiveScreen.Draw(gameTime,_spriteBatch);

            return m_ActiveScreen.Draw(gameTime,_spriteBatch);
       }

       public bool Release()  
        {
            m_ActiveScreen.Release();
            //Though it shouldnt be possible, but still for safeguard
            if(m_InActiveScreen != null)
                m_InActiveScreen.Release();

            return true;
        }

    }
}
